package pers.vic.util.excel.other.demo;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSONObject;

import pers.vic.util.excel.other.ExcelImport;
import pers.vic.util.excel.other.ExcelTplField;

public class ExcelDemo {

	public static void main(String[] args) {
//		importExcel();
		getTxtScore();
	}

	public static void importExcel() {
		List<ScoreModel> result = new ArrayList<ScoreModel>();
		String path = "E:/zbl/2019/11.xlsx";
		File file = new File(path);
		List<String[]> datas = ExcelImport.getExcelData(file);
		// 表头
		List<ExcelTplField> head = ScoreModel.exportExcelTpl();
		if (datas != null && datas.size() > 0) {
			String[] headName = datas.get(1);
			for (int i = 2; i < datas.size(); i++) {
				try {
					ScoreModel info = new ScoreModel();
					System.out.println(JSONObject.toJSON(datas.get(i)));
					ExcelImport.buildObj(info, head, headName, datas.get(i));
					System.out.println(i + " " + info);
					result.add(info);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
		System.out.println("总数:" + result.size());
	}

	public static Map<String, ScoreModel> getTxtScore() {
		int oneRows = 10;
		String path = "E:/zbl/2019/11.txt";
		File file = new File(path);
		try {
			List<String> lines = FileUtils.readLines(file);
			for (int i = 0; i < lines.size(); i = i + oneRows) {
				
				StringBuffer sb = new StringBuffer();
				int rowIndex = 0;
				ScoreModel model = new ScoreModel();
				for (int j = i; j < i + oneRows; j++) {
					String line = lines.get(j);
					sb.append(line).append("-");
					if (rowIndex == 0) {// 第一行 2 1741103302 学号
						if (line != null && line.split("\\s+").length == 2) {
							model.setNo(line.split("\\s+")[1]);
						}
					}
					if (rowIndex == 1) {// 第二行 姓名
						model.setName(line);
					}
					if (rowIndex == 3) {// 第4行 分数1
						model.setScore1(getScore(line));
					}

					if (rowIndex == 4) {// 第5行 分数2
						model.setScore2(getScore(line));
					}
					if (rowIndex == 5) {// 第6行 分数3 或者总分
						if (oneRows == 9) {//总分
							model.setTotal(getScore(line));
						}
						if (oneRows == 10) {
							model.setScore3(getScore(line));
						}
					}
					
					if(rowIndex==6 && oneRows==10 ) {//第七行 总分
						model.setTotal(getScore(line));
					}

					rowIndex++;
				}
				System.err.println(sb);
				System.out.println(JSONObject.toJSON(model));
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static BigDecimal getScore(String line) {
		if (line != null && line != "") {
			try {
				return new BigDecimal(line).setScale(2, BigDecimal.ROUND_HALF_UP);
			} catch (Exception e) {
			}
		}
		return null;
	}

}
